package com.iamback.dy.examine.controller.admin.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamback.dy.config.redis.RedisTemplateUtil;
import com.iamback.dy.entity.ContentExuser;
import com.iamback.dy.entity.DyContexts;
import com.iamback.dy.examine.common.ReidKeyUtil;
import com.iamback.dy.examine.controller.modle.GetContentUnreviewedReq;
import com.iamback.dy.examine.service.IConfigureParameterService;
import com.iamback.dy.examine.service.IContentExuserService;

@Controller
public class ExdyController {
    
	 
	@Autowired
	private IContentExuserService contentExuserService; 
	
	@Autowired
	private RedisTemplateUtil redisTemplateUtil;
	@Autowired
	private IConfigureParameterService configureParameterService;
	
	
	@ResponseBody
	@GetMapping(value = { "/countent/examine/getContentUnreviewed" })
	public Map<String, Object> getdyExaminContent(String dyID) throws InterruptedException{
		Map<String, Object> map = new HashMap<String, Object>();
		if(dyID!=null&&!"".equals(dyID.trim())){
			boolean b =configureParameterService.getAuth(dyID.trim());
		    if(b){
				Long lastContent=999999999999999L;
				
				String contentValue = (String) redisTemplateUtil.get(ReidKeyUtil.last_red_key+dyID.trim());
				if(contentValue!=null&&!contentValue.equals("")){
					lastContent =Long.valueOf(contentValue);
				}
				 
				List<DyContexts> contentExusers=contentExuserService.findContentExuserByDyId(dyID.trim(),lastContent, 0,10);
		        if(contentExusers.isEmpty()||contentExusers.size()==0){//在没有审核分配下 进行延迟  先进行分配再返回
		        	contentExuserService.distributionBy(null,dyID.trim(),0);
		        	Thread.sleep(2000);
		        	contentExusers=contentExuserService.findContentExuserByDyId(dyID.trim(),lastContent, 0,10);
				}else if(contentExusers.size()>0 && contentExusers.size()<10){
				 	contentExuserService.distributionBy(null,dyID.trim(), contentExusers.size());
				}
		        if(contentExusers.size()>0){//
		        	redisTemplateUtil.set(ReidKeyUtil.last_red_key+dyID.trim(),String.valueOf(contentExusers.get((contentExusers.size())-1).getDyContextID()));
		        }else{
		        	redisTemplateUtil.del(ReidKeyUtil.last_red_key+dyID.trim());
		        }
		        map.put("dyContexts",  contentExusers);
		    }else{
		    	map.put("dyContexts",new ArrayList<DyContexts>());
		    }
		}else{
			map.put("dyContexts",new ArrayList<DyContexts>());
		}
        map.put("respMessage", "OK");
        map.put("respCode", "0");
        map.put("isUsed", true);
		return map;
	} 
	
	
	
	
	@ResponseBody
	@PostMapping(value = { "/countent/examine/getContentUnreviewed" })
	public Map<String, Object> dyExaminContent(@RequestBody GetContentUnreviewedReq contentUnreviewedReq) throws InterruptedException{ 

		Map<String, Object> map = new HashMap<String, Object>();
		if(contentUnreviewedReq.getDyID()!=null&&!"".equals(contentUnreviewedReq.getDyID().trim())){
			boolean b =configureParameterService.getAuth(contentUnreviewedReq.getDyID().trim());
		    if(b){
				Long lastContent=999999999999999L;
				
				String contentValue = (String) redisTemplateUtil.get(ReidKeyUtil.last_red_key+contentUnreviewedReq.getDyID().trim());
				if(contentValue!=null&&!contentValue.equals("")){
					lastContent =Long.valueOf(contentValue);
				}
				 
				List<DyContexts> contentExusers=contentExuserService.findContentExuserByDyId(contentUnreviewedReq.getDyID().trim(),lastContent, 0,10);
		        if(contentExusers.isEmpty()||contentExusers.size()==0){//在没有审核分配下 进行延迟  先进行分配再返回
		        	contentExuserService.distributionBy(null,contentUnreviewedReq.getDyID().trim(),0);
		        	Thread.sleep(2000);
		        	contentExusers=contentExuserService.findContentExuserByDyId(contentUnreviewedReq.getDyID().trim(),lastContent, 0,10);
				}else if(contentExusers.size()>0 && contentExusers.size()<10){
				 	contentExuserService.distributionBy(null,contentUnreviewedReq.getDyID().trim(), contentExusers.size());
				}
		        if(contentExusers.size()>0){//
		        	redisTemplateUtil.set(ReidKeyUtil.last_red_key+contentUnreviewedReq.getDyID().trim(),String.valueOf(contentExusers.get((contentExusers.size())-1).getDyContextID()));
		        }else{
		        	redisTemplateUtil.del(ReidKeyUtil.last_red_key+contentUnreviewedReq.getDyID().trim());
		        }
		        map.put("dyContexts",  contentExusers);
		    }else{
		    	map.put("dyContexts",new ArrayList<DyContexts>());
		    }
		}else{
			map.put("dyContexts",new ArrayList<DyContexts>());
		}
        map.put("respMessage", "OK");
        map.put("respCode", "0");
        map.put("isUsed", true);
		return map; 
	}
	
	
//	@PostMapping(value = { "/countent/examine" })
//	public Object dyExamin(String dyId,String coutentId,String operator){
//		ContentExuser contentExuser =new ContentExuser();
//		contentExuser.setOperationCode(UUID.randomUUID().toString()); 
//		contentExuser.setExResult(operator);
//		contentExuser.setExTime(new Date());
//		contentExuser.setExuserId(Long.valueOf("0"));
//		contentExuser.setExdyId(dyId);
//		contentExuser.setDyContentId(Long.valueOf(coutentId));
//	    Map<String, Object> map=contentExuserService.updateExDyIdContent(contentExuser);
//	     
//		return map;
//	} 
}
